﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace utd_hrrs
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection conn;
        String user_name;
        Boolean billing_info;

        protected void Page_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection("Data Source=rockstar.hopto.org,11433;Initial Catalog=UTD_HRRS;Persist Security Info=True;User ID=utd_hrrs;Password=bhowmik6360");
            conn.Open();
            user_name = Request.Cookies["username"].Value;
            SqlCommand command = new SqlCommand("SELECT * from user_data where user_id = '" + user_name + "'", conn);
            SqlDataReader dr = command.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                username.Text = dr.GetString(0);
                name.Text = dr.GetString(1);
                staff_id.Text = dr.GetString(4);
                department.Text = dr.GetString(5);
                office_phone.Text = dr.GetString(6);
                campus.Text = dr.GetString(7);
                phone_ext.Text = dr.GetString(8);
                room.Text = dr.GetString(9);
            }
            dr.Close();

            command = new SqlCommand("SELECT * FROM BILLING WHERE USER_ID = '" + user_name + "'", conn);
            dr = command.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                street.Text = dr.GetString(1);
                zip_code.Text = dr.GetString(2);
                state.Text = dr.GetString(3);
                credit.Text = dr.GetString(4);
                billing_info = true;
            }
            else
            {
                billing_info = false;
            }
            dr.Close();
        }

        protected void updateClick(object sender, EventArgs e)
        {
            String rm = Request.Form["room"];
            String off_phone = Request.Form["office_phone"];
            String ext = Request.Form["phone_ext"];

            SqlCommand command = new SqlCommand("Update user_data set off_phone = '" + off_phone + 
                "', phone_ext = '" + ext + "', room_num = '" + rm + "' where user_id = '" + user_name + "'", conn);
            int rows = command.ExecuteNonQuery();
            if (rows > 0)
            {
                error.Visible = false;
                success.Visible = true;
                Page_Load(sender, e);
            }
            else
            {
                error.Visible = true;
                success.Visible = false;
            }
        }

        protected void updateBillClick(object sender, EventArgs e)
        {
            String str = Request.Form["street"];
            String zip = Request.Form["zip_code"];
            String st = Request.Form["state"];
            String cc = Request.Form["credit"];
            SqlCommand command = null;

            if (billing_info)
            {
                command = new SqlCommand("Update billing set street = '" + str +
                    "', zip_code = '" + zip + "', state = '" + st + "', payment_method = '" + cc +
                    "' where user_id = '" + user_name + "'", conn);
            }
            else
            {
                command = new SqlCommand("Insert into billing (street, zip_code, state, payment_method, user_id) values ('" +
                    str + "', '" + zip + "', '" + st + "', '" + cc + "', '" + user_name + "')", conn);
            }
            int rows = command.ExecuteNonQuery();
            if (rows > 0)
            {
                bill_error.Visible = false;
                bill_success.Visible = true;
                Page_Load(sender, e);
            }
            else
            {
                bill_error.Visible = true;
                bill_success.Visible = false;
            }
        }
    }
}